## 

rm(list = ls())

library(tidyverse)

## Get data
## Note that this figure requires data that we cannot make available publicly

f <- read_rds('data_private/nexis_data_flooding.rds')
f_climate <- read_rds('data_private/nexis_data_climate.rds')

## Monthly intervals

start <- as.Date("2021-01-01")
end <- as.Date("2021-10-01")

library(lubridate)
dates_m <- floor_date(seq(start, end, by = 'month'), 
                      unit = "month") + 11

## Convert to months

f <- f %>% 
  mutate(month_10 = cut(date, dates_m)) %>% 
  mutate(month_10 = as.numeric(as.factor(month_10)) - 7) %>% 
  filter(!is.na(month_10))

f_climate <- f_climate %>% 
  mutate(month_10 = cut(date, dates_m)) %>% 
  mutate(month_10 = as.numeric(as.factor(month_10)) - 7) %>% 
  filter(!is.na(month_10))

## Aggregate by month

f_agg <- f %>% 
  group_by(month_10) %>% 
  summarise(n = n()) %>% 
  ungroup()

f_agg_clim <- f_climate %>% 
  group_by(month_10) %>% 
  summarise(n_clim = n()) %>% 
  ungroup() %>% 
  mutate(n_clim = ifelse(is.na(n_clim), 0, n_clim))

## Merge

f_merged <- f_agg %>% 
  left_join(f_agg_clim) %>% 
  mutate(share_clim = n_clim / n) %>% 
  mutate(share_clim = ifelse(share_clim > 1, 1, share_clim))

## Prep for plot

f_merged <- f_merged %>% 
  dplyr::select(-n_clim) %>% 
  filter(!month_10 < -1) %>% 
  mutate(share_clim = share_clim * 100) %>% 
  pivot_longer(cols = -month_10) %>% 
  mutate(name = ifelse(name == 'n',
                       "Number of articles\nmentioning flooding",
                       "Share of articles on flooding\nalso mentioning climate change (%)"))

## Plot

f_merged %>% ggplot(aes(month_10, value)) +
  geom_vline(xintercept = -0.5, linetype = 'dotted') +
  geom_line() +
  geom_point(shape = 21, fill = 'white', size = 2)+
  facet_wrap(~name, scales = 'free_y') +
  theme_bw() +
  xlab("Months relative to flood") + 
  theme(axis.title.y = element_blank())
